local skel = fk.CreateSkill{
  name = "ksp__poyuan",
  tags = { Skill.Compulsory, Skill.Switch },
}

local DIY = require "packages/diy_utility/diy_utility"
local KleeU = require "packages/klee_xy_I/utility/k_utility"

Fk:loadTranslationTable{
  ["ksp__poyuan"] = "破渊",
  [":ksp__poyuan"] = "锁定技，转换技，你每回合第①②③次造成伤害时，防止之并选择一项：1.将手牌摸至上限；2.获得其装备区所有牌；3.回复1点体力。周始：背水：入魔。",
  ["#ksp__poyuan"] = "破渊：防止此伤害并选择一项：",
  ["#ksp__poyuan_ask2"] = "周始：背水：入魔",
  ["#ksp__poyuan_c1"] = "将手牌摸至上限",
  ["#ksp__poyuan_c2"] = "获得其装备区所有牌",
  ["#ksp__poyuan_c3"] = "回复1点体力",

  ["$ksp__poyuan1"] = "你的脊梁，不堪一击！",
  ["$ksp__poyuan2"] = "你的胆气，一文不值！",
}

skel:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player,skel.name,1,3)
end)

skel:addLoseEffect(function (self, player, is_death)
  DIY.removeSwitchSkill(player, self.name)
end)

skel:addEffect(fk.DamageCaused,{
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local damage_events = player.room.logic:getEventsOfScope(GameEvent.Damage, 3, function (e)
        return e.data.from == player
      end, player.HistoryTurn)
      local n = DIY.getSwitchState(player,skel.name)
      if #damage_events >= n and damage_events[n].data == data then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data:preventDamage()
    DIY.changeSwitchState(player,skel.name)
    --
    local allChoices = {"#ksp__poyuan_c1","#ksp__poyuan_c2","#ksp__poyuan_c3"}
    local choices = {}
    if #player:getCardIds("h") < player.maxHp then
      table.insertIfNeed(choices,"#ksp__poyuan_c1")
    end
    if #data.to:getCardIds("e") > 0 then
      table.insertIfNeed(choices,"#ksp__poyuan_c2")
    end
    if player:isWounded() then
      table.insertIfNeed(choices,"#ksp__poyuan_c3")
    end
    local choose
    if #choices == 0 then
      return
    elseif player:getMark("ksp__poyuan_beishui-turn") > 0 then--周始
      room:setPlayerMark(player,"ksp__poyuan_beishui-turn",0)
      if #data.to:getCardIds("e") > 0 then
        choose = "#ksp__poyuan_c2"
      end
    elseif #choices == 1 then
      choose = choices[1]
    else
      choose = room:askToChoice(player,{
        choices = choices,
        skill_name = skel.name,
        prompt = "#ksp__poyuan",
        all_choices = allChoices,
        cancelable = false,
      })
    end
    --
    if choose == allChoices[1] then
      --摸牌
      local n = player.maxHp - #player:getCardIds("h")
      room:drawCards(player,n,skel.name)
    elseif choose == allChoices[2] then
      --获得装备牌
      local cards = data.to:getCardIds("e")
      room:moveCardTo(cards,Player.Hand,player,fk.ReasonJustMove,skel.name,nil,true,player)
    elseif choose == allChoices[3] then
      --回血
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = skel.name
      }
    end
  end,
})

skel:addEffect(DIY.SkillSwitchLoopback,{
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and data.skill.name == skel.name and player:hasSkill(skel.name) and not player:hasSkill("#rumo", true)
  end,
  on_cost = function (self, event, target, player, data)
    local YN = KleeU.askYesOrNo(player,"#ksp__poyuan_ask2",skel.name)
    return YN
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "#rumo", nil, false, true)--入魔
    --
    if #player:getCardIds("h") < player.maxHp then
      local n = player.maxHp - #player:getCardIds("h")
      room:drawCards(player,n,skel.name)
    end
    --
    if player:isWounded() then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = skel.name
      }
    end
    room:setPlayerMark(player,"ksp__poyuan_beishui-turn",1)--周始把一三选项处理了，然后二选项在上面自动执行（如果有）
  end,
})

return skel